﻿using CCWin;
using SuperMap.Data;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MDF
{
    /// <summary>
    /// 数据融合窗口
    /// </summary>
    public partial class SJRH : CCSkinMain
    {
        //xml读写对象
        private IO_xml m_xml;
        private Datasources m_datasources;
        private List<DatasetVector[]> m_SetToSets;
        private CancellationTokenSource m_Cancellation;
        /// <summary>
        /// 数据融合窗口初始化
        /// </summary>
        public SJRH(Datasources datasources)
        {
            InitializeComponent();
            //实例化xml读写对象
            m_xml = new IO_xml("ShuJuRongHe");
            m_datasources = datasources;

            //填充数据
            FillSetComboBox();
            FillSourceComboBox();
            FillFusionModeComboBox();
        }
        #region 数据集和选择集
        /// <summary>
        /// 填充数据集下拉框选择项
        /// </summary>
        private void FillSetComboBox()
        {
            try
            {
                //确保清空
                this.Set1_Combo.Items.Clear();
                this.Set2_Combo.Items.Clear();
                //是否勾选数据集或选择集
                if (this.Sets_CheckBox.Checked)
                {
                    //填充选择集
                    //实例化数据信息读取对象
                    DataInfo m_datainfo = new DataInfo();
                    //获取选择集的名字并填充
                    foreach (string SelectionsName in m_datainfo.GetAllSelectionsNames())
                    {
                        this.Set1_Combo.Items.Add(SelectionsName);
                        this.Set2_Combo.Items.Add(SelectionsName);
                    }
                }
                else
                {
                    //遍历填充数据集
                    foreach (Datasource datasource in m_datasources)
                    {
                        foreach (Dataset dataset in datasource.Datasets)
                        {
                            this.Set1_Combo.Items.Add(dataset.Name);
                            this.Set2_Combo.Items.Add(dataset.Name);
                        }
                    }
                }
                //默认选择项
                if (this.Set1_Combo.Items.Count > 0)
                {
                    this.Set1_Combo.SelectedIndex = 0;
                    this.Set2_Combo.Items.RemoveAt(0);
                    if (this.Set2_Combo.Items.Count > 0)
                    {
                        this.Set2_Combo.SelectedIndex = 0;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改set1选择项时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Set1_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //获取set2选项
                string text2 = this.Set2_Combo.Text;
                //清空set2选项，并重新填充
                this.Set2_Combo.Items.Clear();
                foreach (object item in this.Set1_Combo.Items)
                {
                    this.Set2_Combo.Items.Add(item);
                }
                //如果选择项相同
                if (this.Set1_Combo.Text == text2)
                {
                    //在set2选项中移除
                    this.Set2_Combo.Items.Remove(text2);
                    //默认选择项
                    if (this.Set2_Combo.Items.Count > 0)
                    {
                        this.Set2_Combo.SelectedIndex = 0;
                    }

                }
                //选择项不同
                else
                {
                    //还原选项
                    this.Set2_Combo.Text = text2;
                }
                //重新检查
                this.Yes_Button.Enabled = false;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 修改set2选择项时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Set2_Combo_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //重新检查
                this.Yes_Button.Enabled = false;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 切换数据集或选择集
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Sets_CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.Sets_CheckBox.Checked)
                {
                    this.Sets_CheckBox.Text = "选择集";
                }
                else
                {
                    this.Sets_CheckBox.Text = "数据集";
                }
                //填充数据
                FillSetComboBox();
                //重新检查
                this.Yes_Button.Enabled = false;
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region 数据源
        /// <summary>
        /// 填充数据源
        /// </summary>
        private void FillSourceComboBox()
        {
            try
            {
                //确保清空
                this.Source_ComboBox.Items.Clear();
                //填充数据源名称
                foreach (Datasource datasource in m_datasources)
                {
                    this.Source_ComboBox.Items.Add(datasource.Alias);
                }
                //默认选择项
                if (this.Source_ComboBox.Items.Count > 0)
                {
                    this.Source_ComboBox.SelectedIndex = 0;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region 融合模式
        /// <summary>
        /// 填充融合模式
        /// </summary>
        private void FillFusionModeComboBox()
        {
            try
            {
                //填充匹配模式控件
                object[] items = new object[] { "无", "几何", "属性", "拓扑结构" };
                this.FirstMatching_ComboBox.Items.AddRange(items);
                this.SecondMatching_ComboBox.Items.AddRange(items);
                this.ThirdMatching_ComboBox.Items.AddRange(items);
                //读取融合模式的配置信息并填充
                string[] modeInfos = m_xml.ReadChildNodesName("FusionModes");
                foreach (string modeInfo in modeInfos)
                {
                    this.FusionMode_ComboBox.Items.Add(modeInfo);
                    this.UserModeName_ComboBox.Items.Add(modeInfo);
                }
                //默认选项
                if (this.FusionMode_ComboBox.Items.Count > 0)
                {
                    this.FusionMode_ComboBox.SelectedIndex = 0;
                    this.UserModeName_ComboBox.Items.RemoveAt(0);
                    //默认值
                    this.FirstMatching_ComboBox.SelectedIndex = 1;
                    this.SecondMatching_ComboBox.SelectedIndex = 2;
                    this.ThirdMatching_ComboBox.SelectedIndex = 3;
                    this.FirstWeight_TextBox.Text = "0.5";
                    this.SecondWeight_TextBox.Text = "0.3";
                    this.ThirdWeight_TextBox.Text = "0.2";
                    this.Threshold_TextBox.Text = "0.8";
                } 
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 自定义下拉选择融合模式时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //获取模式信息
                ComboBox m_comboBox = (ComboBox)sender;
                if (m_comboBox.SelectedItem.ToString() != "")
                {
                    List<string[]> modeInfos = m_xml.ReadChildValues(m_comboBox.SelectedItem.ToString());
                    //填充信息
                    foreach (string[] modeInfo in modeInfos)
                    {
                        switch (modeInfo[0])
                        {
                            case "第一匹配":
                                this.FirstMatching_ComboBox.SelectedItem = modeInfo[1];
                                this.FirstWeight_TextBox.Text = modeInfo[2];
                                break;
                            case "第二匹配":
                                this.SecondMatching_ComboBox.SelectedItem = modeInfo[1];
                                this.SecondWeight_TextBox.Text = modeInfo[2];
                                break;
                            case "第三匹配":
                                this.ThirdMatching_ComboBox.SelectedItem = modeInfo[1];
                                this.ThirdWeight_TextBox.Text = modeInfo[2];
                                break;
                            default:
                                break;
                        }
                    }
                    //重新检查
                    this.Yes_Button.Enabled = false;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 激活自定义
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserDefined_CheckBox_CheckedChanged(object sender, EventArgs e)
        {
            try
            {
                if (this.UserDefined_CheckBox.Checked)
                {
                    //激活自定义的功能区
                    this.UserModeName_ComboBox.Enabled = true;
                    this.FirstMatching_ComboBox.Enabled = true;
                    this.SecondMatching_ComboBox.Enabled = true;
                    this.ThirdMatching_ComboBox.Enabled = true;
                    this.FusionMode_ComboBox.Enabled = false;
                    //功能键
                    this.Delete_Button.Enabled = true;
                    this.Save_Button.Enabled = true;
                    this.Check_Button.Enabled = false;
                    this.Yes_Button.Enabled = false;
                    //初值
                    this.UserModeName_ComboBox.Text = "";
                    this.FirstMatching_ComboBox.SelectedIndex = 0;
                    this.SecondMatching_ComboBox.SelectedIndex = 0;
                    this.ThirdMatching_ComboBox.SelectedIndex = 0;
                    this.FirstWeight_TextBox.Text = "";
                    this.SecondWeight_TextBox.Text = "";
                    this.ThirdWeight_TextBox.Text = "";
                    //激活下拉限制
                    this.FirstMatching_ComboBox.SelectionChangeCommitted += new System.EventHandler(this.Matching_ComboBox_SelectionChangeCommitted);
                    this.SecondMatching_ComboBox.SelectionChangeCommitted += new System.EventHandler(this.Matching_ComboBox_SelectionChangeCommitted);
                    this.ThirdMatching_ComboBox.SelectionChangeCommitted += new System.EventHandler(this.Matching_ComboBox_SelectionChangeCommitted);

                }
                else
                {
                    //取消自定义的功能区
                    //还原初值
                    this.UserModeName_ComboBox.Text = "";
                    if(this.FusionMode_ComboBox.Text != "")
                    {
                        List<string[]> modeInfos = m_xml.ReadChildValues(this.FusionMode_ComboBox.Text);
                        //填充信息
                        foreach (string[] modeInfo in modeInfos)
                        {
                            switch (modeInfo[0])
                            {
                                case "第一匹配":
                                    this.FirstMatching_ComboBox.SelectedItem = modeInfo[1];
                                    this.FirstWeight_TextBox.Text = modeInfo[2];
                                    break;
                                case "第二匹配":
                                    this.SecondMatching_ComboBox.SelectedItem = modeInfo[1];
                                    this.SecondWeight_TextBox.Text = modeInfo[2];
                                    break;
                                case "第三匹配":
                                    this.ThirdMatching_ComboBox.SelectedItem = modeInfo[1];
                                    this.ThirdWeight_TextBox.Text = modeInfo[2];
                                    break;
                                default:
                                    break;
                            }
                        }
                    }
                    else
                    {
                        //默认模式
                        this.FusionMode_ComboBox.SelectedIndex = 0;
                        this.FirstMatching_ComboBox.SelectedIndex = 1;
                        this.SecondMatching_ComboBox.SelectedIndex = 2;
                        this.ThirdMatching_ComboBox.SelectedIndex = 3;
                        this.FirstWeight_TextBox.Text = "0.5";
                        this.SecondWeight_TextBox.Text = "0.3";
                        this.ThirdWeight_TextBox.Text = "0.2";
                    }

                    this.UserModeName_ComboBox.Enabled = false;
                    this.FirstMatching_ComboBox.Enabled = false;
                    this.SecondMatching_ComboBox.Enabled = false;
                    this.ThirdMatching_ComboBox.Enabled = false;
                    this.FirstWeight_TextBox.Enabled = false;
                    this.SecondWeight_TextBox.Enabled = false;
                    this.ThirdWeight_TextBox.Enabled = false;
                    this.FusionMode_ComboBox.Enabled = true;
                    //功能键
                    this.Delete_Button.Enabled = false;
                    this.Save_Button.Enabled = false;
                    this.Check_Button.Enabled = true;
                    //移除下拉限制
                    this.FirstMatching_ComboBox.SelectionChangeCommitted -= new System.EventHandler(this.Matching_ComboBox_SelectionChangeCommitted);
                    this.SecondMatching_ComboBox.SelectionChangeCommitted -= new System.EventHandler(this.Matching_ComboBox_SelectionChangeCommitted);
                    this.ThirdMatching_ComboBox.SelectionChangeCommitted -= new System.EventHandler(this.Matching_ComboBox_SelectionChangeCommitted);
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 自定义名称输入限制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void UserModeName_ComboBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                //Regex reg = new Regex("^[a-zA-Z0-9\u4e00-\u9fa5]$");
                Regex reg = new Regex("^\\w$");
                if (this.UserModeName_ComboBox.Text == "" && e.KeyChar >= '0' && e.KeyChar <= '9')
                {
                    e.Handled = true;
                }
                else if (!reg.IsMatch(e.KeyChar.ToString()) && e.KeyChar != '\b')
                {
                    e.Handled = true;
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 控制选择模式的权重填写
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Matching_ComboBox_SelectionChangeCommitted(object sender, EventArgs e)
        {
            try
            {
                //控制对应的权值是否可以填写
                ComboBox m_comboBox = (ComboBox)sender;
                if (m_comboBox.SelectedIndex > 0)
                {
                    if (m_comboBox.Name.Contains("First"))
                    {
                        this.FirstWeight_TextBox.Enabled = true;
                    }
                    else if (m_comboBox.Name.Contains("Second"))
                    {
                        this.SecondWeight_TextBox.Enabled = true;
                    }
                    else
                    {
                        this.ThirdWeight_TextBox.Enabled = true;
                    }
                }
                else
                {
                    if (m_comboBox.Name.Contains("First"))
                    {
                        this.FirstWeight_TextBox.Text = "";
                        this.FirstWeight_TextBox.Enabled = false;
                    }
                    else if (m_comboBox.Name.Contains("Second"))
                    {
                        this.SecondWeight_TextBox.Text = "";
                        this.SecondWeight_TextBox.Enabled = false;
                    }
                    else
                    {
                        this.ThirdWeight_TextBox.Text = "";
                        this.ThirdWeight_TextBox.Enabled = false;
                    }
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 权重输入限制
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Weight_TextBox_KeyPress(object sender, KeyPressEventArgs e)
        {
            try
            {
                TextBox m_textBox = (TextBox)sender;
                string text = m_textBox.Text;
                if (text == "" && !(e.KeyChar >= '0' && e.KeyChar <= '1'))
                {
                    e.Handled = true;
                }
                else if (!((e.KeyChar >= '0' && e.KeyChar <= '9') || e.KeyChar == '.' || e.KeyChar == (char)8))
                {
                    e.Handled = true;
                }
                else
                {
                    if (e.KeyChar >= '0' && e.KeyChar <= '9')
                    {
                        if (text == "" || text == "0")
                        {
                            e.Handled = true;
                            if (e.KeyChar >= '0' && e.KeyChar <= '1')
                            {
                                
                                m_textBox.Text = (e.KeyChar - 48).ToString();
                            }
                        }
                        else
                        {
                            text += (e.KeyChar - 48);
                            double num = double.Parse(text);
                            if (num < 0 || num > 1)
                            {
                                e.Handled = true;
                                
                            }
                        }
                    }
                    if (e.KeyChar == '.')
                    {
                        int num = text.IndexOf('.');
                        if (text.Length == 0 || num != -1)
                        {
                            e.Handled = true;
                        }
                        if (e.KeyChar == (char)8 && text.Length == 1)
                        {
                            e.Handled = true;
                            m_textBox.Text = "0";
                        }
                    }
                }
                m_textBox.SelectionStart = m_textBox.Text.Length;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 删除自定义模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Delete_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //判断名称非空
                if (this.UserModeName_ComboBox.Text != "")
                {
                    if (MessageBox.Show("确定删除：" + this.UserModeName_ComboBox.Text + "？", "提示：",MessageBoxButtons.OKCancel) == DialogResult.OK)
                    {
                        if (m_xml.DeleteXml("FusionModes", this.UserModeName_ComboBox.Text))
                        {
                            MessageBox.Show("删除完成！", "提示：");
                            //更新填充内容
                            this.FusionMode_ComboBox.Items.Remove(this.UserModeName_ComboBox.Text);
                            this.UserModeName_ComboBox.Items.Remove(this.UserModeName_ComboBox.Text);
                            //恢复初值
                            this.UserModeName_ComboBox.Text = "";
                            this.FirstMatching_ComboBox.SelectedIndex = 0;
                            this.SecondMatching_ComboBox.SelectedIndex = 0;
                            this.ThirdMatching_ComboBox.SelectedIndex = 0;
                            this.FirstWeight_TextBox.Text = "";
                            this.SecondWeight_TextBox.Text = "";
                            this.ThirdWeight_TextBox.Text = "";
                        } 
                    }
                }
                else
                {
                    MessageBox.Show("被删除模式名称为空！", "警告：");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 保存自定义模式
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Save_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //判断名称非空
                if (this.UserModeName_ComboBox.Text != "")
                {
                    //判断是否选择第一匹配条件
                    if (this.FirstMatching_ComboBox.SelectedIndex > 0)
                    {
                        //判断权值是否合规
                        double weight = 0.0;
                        if (this.FirstWeight_TextBox.Text != "")
                        {
                            weight += double.Parse(this.FirstWeight_TextBox.Text);
                        }
                        if (this.SecondWeight_TextBox.Text != "")
                        {
                            weight += double.Parse(this.SecondWeight_TextBox.Text);
                        }
                        if (this.ThirdWeight_TextBox.Text != "")
                        {
                            weight += double.Parse(this.ThirdWeight_TextBox.Text);
                        }
                        if (weight == 1.0)
                        {
                            //判断二三匹配条件是否选择
                            if (this.SecondMatching_ComboBox.SelectedIndex != -1 && this.ThirdMatching_ComboBox.SelectedIndex != -1)
                            {
                                //保存或修改数据
                                //保存第一匹配
                                string ChildName = "第一匹配";
                                string[] m_keys = new string[] { "模式","权值" };
                                string[] m_values = new string[] { this.FirstMatching_ComboBox.Text, this.FirstWeight_TextBox.Text };
                                m_xml.WriteXml("FusionModes", this.UserModeName_ComboBox.Text, ChildName, m_keys, m_values);
                                //保存第二匹配
                                ChildName = "第二匹配";
                                m_values = new string[] { this.SecondMatching_ComboBox.Text, this.SecondWeight_TextBox.Text };
                                m_xml.WriteXml("FusionModes", this.UserModeName_ComboBox.Text, ChildName, m_keys, m_values);
                                //保存第三匹配
                                ChildName = "第三匹配";
                                m_values = new string[] { this.ThirdMatching_ComboBox.Text, this.ThirdWeight_TextBox.Text };
                                m_xml.WriteXml("FusionModes", this.UserModeName_ComboBox.Text, ChildName, m_keys, m_values);

                                MessageBox.Show("保存完成！", "提示：");
                                //更新填充
                                this.FusionMode_ComboBox.Items.Add(this.UserModeName_ComboBox.Text);
                                this.UserModeName_ComboBox.Items.Add(this.UserModeName_ComboBox.Text);
                            }
                        }
                        else
                        {
                            MessageBox.Show("权值的和需要等于1！", "警告：");
                        }
                    }
                    else
                    {
                        MessageBox.Show("第一匹配条件不能为空！", "警告：");
                    }
                }
                else
                {
                    MessageBox.Show("自定义模式名称为空！", "警告：");
                }

            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion

        #region 检查、执行、取消
        /// <summary>
        /// 当前设置的合法性检查
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Check_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //实例化检查类
                DataCheck dataCheck = new DataCheck(m_datasources);
                //检查结果记录
                StringBuilder stringBuilder = new StringBuilder();
                //检查设置是否合法（保存数据集名称非空）
                if (this.SetName_TextBox.Text == "")
                {
                    stringBuilder.AppendLine("保存数据集名称为空！");
                }
                else
                {
                    //判断数据集类型是否合法
                    string matchResult = dataCheck.CheckMatchInfo(this.Sets_CheckBox.Checked, this.Set1_Combo.Text, this.Set2_Combo.Text);
                    if (!string.IsNullOrEmpty(matchResult))
                    {
                        stringBuilder.AppendLine(matchResult);
                    }
                    if (dataCheck.m_SetToSet != null && dataCheck.m_SetToSet.Count > 0)
                    {
                        m_SetToSets = dataCheck.m_SetToSet;
                        //根据模式判断阈值信息
                        int[] modeFusion = new int[] { this.FirstMatching_ComboBox.SelectedIndex, this.SecondMatching_ComboBox.SelectedIndex, this.ThirdMatching_ComboBox.SelectedIndex };
                        stringBuilder.AppendLine(dataCheck.CheckYuzhiInfo(modeFusion));
                    }
                }
                //删除空行
                string result = stringBuilder.ToString();
                while (true)
                {
                    if (result.Contains("\n\r"))
                    {
                        result = result.Replace("\n\r", "");
                    }
                    else
                    {
                        break;
                    }
                }
                if (string.IsNullOrWhiteSpace(result))
                {
                    this.Yes_Button.Enabled = true;
                    MessageBox.Show("检查完成！", "提示：");
                }
                else if (result.Contains("未完全匹配")|| result.Contains("不存在映射信息"))
                {
                    this.Yes_Button.Enabled = true;
                    result = result + "但您仍可以继续执行计算。";
                    MessageBox.Show(result, "警告：");
                }
                else
                {
                    MessageBox.Show(result, "警告：");
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 按照当前设置执行匹配融合
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Yes_Button_Click(object sender, EventArgs e)
        {
            try
            {
                //获取保存数据源和名字
                string saveSourceName = this.Source_ComboBox.Text;
                string saveDatasetName = this.SetName_TextBox.Text;
                //实例化数据融合类
                DataFusion dataFusion = new DataFusion(m_datasources);
                m_Cancellation = new CancellationTokenSource();
                //获取当前模式和权值
                int[] modeFusion = new int[] { this.FirstMatching_ComboBox.SelectedIndex, this.SecondMatching_ComboBox.SelectedIndex, this.ThirdMatching_ComboBox.SelectedIndex };
                double[] matchWeigth = GetMatchWeigth();
                bool IsSelections = this.Sets_CheckBox.Checked;
                string[] selectinosNames = new string[] { this.Set1_Combo.Text, this.Set2_Combo.Text };
                //初始化数据计算总量和进度
                StartValueSum(modeFusion);
                //查询是数据集内容否已进行过匹配
                if (MDF_Main.mDF_Main.datasetsMatchResults != null)
                {
                    //获取已匹配的数据
                    foreach (DatasetsMatchResult matchResult in MDF_Main.mDF_Main.datasetsMatchResults)
                    {
                        string set12Name = matchResult.Set1Name + matchResult.Set2Name;
                        foreach (DatasetVector[] SetToSet in m_SetToSets)
                        {
                            //有完成的匹配，则根据现在的模式，继续完成其他匹配，并融合
                            if (set12Name == SetToSet[0].Name + SetToSet[1].Name)
                            {
                                //执行匹配
                                Task task = new Task(() => dataFusion.RunDataMatch(modeFusion, matchResult, SetToSet, IsSelections, selectinosNames, m_Cancellation.Token), m_Cancellation.Token);
                                task.Start();
                                ProgressForm progressForm = new ProgressForm() { Text = "" };
                                if (progressForm.ShowDialog() != DialogResult.OK)
                                {
                                    m_Cancellation.Cancel();
                                    MessageBox.Show("已取消！");
                                }
                                else
                                {
                                    //初始化融合进度
                                    StartValueSum(new int[] { 5 });
                                    //执行融合
                                    Task taskFusion = new Task(() => dataFusion.RunDataFusion(saveSourceName, saveDatasetName, matchWeigth, m_Cancellation.Token), m_Cancellation.Token);
                                    taskFusion.Start();
                                    ProgressForm progressFormFusion = new ProgressForm() { Text = "SJRH" };
                                    if (progressFormFusion.ShowDialog() != DialogResult.OK)
                                    {
                                        m_Cancellation.Cancel();
                                        MessageBox.Show("已取消！");
                                    }
                                    else
                                    {
                                        this.Yes_Button.Enabled = false;
                                        MessageBox.Show("融合完成", "提示：");
                                    }
                                    progressFormFusion.Dispose();
                                }
                                progressForm.Dispose();
                            }
                            //没有完成匹配，则按照当前模式进行匹配
                            else
                            {
                                //新实例化一个匹配结果对象
                                DatasetsMatchResult newMatchResult = new DatasetsMatchResult() { Set1Name = SetToSet[0].Name, Set2Name = SetToSet[1].Name };
                                //添加到全局
                                MDF_Main.mDF_Main.datasetsMatchResults.Add(newMatchResult);
                                //执行匹配
                                Task task = new Task(() => dataFusion.RunDataMatch(modeFusion, matchResult, SetToSet, IsSelections, selectinosNames, m_Cancellation.Token), m_Cancellation.Token);
                                task.Start();
                                ProgressForm progressForm = new ProgressForm() { Text = "" };
                                if (progressForm.ShowDialog() != DialogResult.OK)
                                {
                                    m_Cancellation.Cancel();
                                    MessageBox.Show("已取消！");
                                }
                                else
                                {
                                    //初始化融合进度
                                    StartValueSum(new int[] { 5 });
                                    //执行融合
                                    Task taskFusion = new Task(() => dataFusion.RunDataFusion(saveSourceName, saveDatasetName, matchWeigth, m_Cancellation.Token), m_Cancellation.Token);
                                    taskFusion.Start();
                                    ProgressForm progressFormFusion = new ProgressForm() { Text = "SJRH" };
                                    if (progressFormFusion.ShowDialog() != DialogResult.OK)
                                    {
                                        m_Cancellation.Cancel();
                                        MessageBox.Show("已取消！");
                                    }
                                    else
                                    {
                                        this.Yes_Button.Enabled = false;
                                        MessageBox.Show("融合完成", "提示：");
                                    }
                                    progressFormFusion.Dispose();
                                }
                                progressForm.Dispose();
                            }
                        }
                    }
                }
                //在系统中没有进行过任何匹配，则按照当前模式进行匹配
                else
                {
                    foreach (DatasetVector[] SetToSet in m_SetToSets)
                    {
                        //新实例化一个匹配结果对象
                        DatasetsMatchResult newMatchResult = new DatasetsMatchResult() { Set1Name = SetToSet[0].Name, Set2Name = SetToSet[1].Name };
                        //添加到全局
                        MDF_Main.mDF_Main.DatasetsMatchResultAdd(newMatchResult);
                        //执行匹配
                        Task task = new Task(() => dataFusion.RunDataMatch(modeFusion, newMatchResult, SetToSet, IsSelections, selectinosNames, m_Cancellation.Token), m_Cancellation.Token);
                        task.Start();
                        ProgressForm progressForm = new ProgressForm() { Text = "" };
                        if (progressForm.ShowDialog() != DialogResult.OK)
                        {
                            m_Cancellation.Cancel();
                            MessageBox.Show("已取消！");
                        }
                        else
                        {
                            //初始化融合进度
                            StartValueSum(new int[] { 5 });
                            //执行融合
                            Task taskFusion = new Task(() => dataFusion.RunDataFusion(saveSourceName, saveDatasetName, matchWeigth, m_Cancellation.Token), m_Cancellation.Token);
                            taskFusion.Start();
                            ProgressForm progressFormFusion = new ProgressForm() { Text = "SJRH" };
                            if (progressFormFusion.ShowDialog() != DialogResult.OK)
                            {
                                m_Cancellation.Cancel();
                                MessageBox.Show("已取消！");
                            }
                            else
                            {
                                this.Yes_Button.Enabled = false;
                                MessageBox.Show("融合完成", "提示：");
                            }
                            progressFormFusion.Dispose();
                        }
                        progressForm.Dispose();
                    }
                }
                //刷新树视图
                MDF_Main.mDF_Main.RefreshWorkspaceTree();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 取消并返回
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void No_Button_Click(object sender, EventArgs e)
        {
            try
            {
                this.DialogResult = DialogResult.No;
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取权值
        /// </summary>
        /// <returns></returns>
        private double[] GetMatchWeigth()
        {
            try
            {
                //记录权重值
                List<double> matchWeigth = new List<double>();
                //第一匹配权值
                if (this.FirstWeight_TextBox.Text != "")
                {
                    matchWeigth.Add(double.Parse(this.FirstWeight_TextBox.Text));
                }
                else
                {
                    matchWeigth.Add(0.0);
                }
                //第二匹配权值
                if (this.SecondWeight_TextBox.Text != "")
                {
                    matchWeigth.Add(double.Parse(this.SecondWeight_TextBox.Text));
                }
                else
                {
                    matchWeigth.Add(0.0);
                }
                //第三配权值
                if (this.ThirdWeight_TextBox.Text != "")
                {
                    matchWeigth.Add(double.Parse(this.ThirdWeight_TextBox.Text));
                }
                else
                {
                    matchWeigth.Add(0.0);
                }
                //返回结果
                return matchWeigth.ToArray();
            }
            catch (Exception)
            {

                throw;
            }
        }
        /// <summary>
        /// 获取数据集合的数据计算总量
        /// </summary>
        /// <param name="modeFusion">当前模式</param>
        private void StartValueSum(int[] modeFusion)
        {
            try
            {
                //实例化信息类
                DataInfo m_dataInfo = new DataInfo(m_datasources);
                //获取数据集合的数据计算总量
                DatasetsMatchResult.SetsRecordCountSum = 0;

                DatasetsMatchResult.SpaceProgressValue = 100;
                DatasetsMatchResult.AttributeProgressValue = 100;
                DatasetsMatchResult.TopollogyProgressValue = 100;
                if (this.Sets_CheckBox.Checked)
                {
                    foreach (DatasetVector[] setToset in m_SetToSets)
                    {
                        DatasetsMatchResult.SetsRecordCountSum += m_dataInfo.GetSelectionDataIDs(this.Set1_Combo.Text, setToset[0].Name).Length * m_dataInfo.GetSelectionDataIDs(this.Set2_Combo.Text, setToset[1].Name).Length;
                    }
                }
                else
                {
                    foreach (DatasetVector[] setToset in m_SetToSets)
                    {
                        DatasetsMatchResult.SetsRecordCountSum += setToset[0].RecordCount * setToset[1].RecordCount;
                    }
                }
                //根据模式进行初始化
                foreach (int mode in modeFusion)
                {
                    switch (mode)
                    {
                        case 1:
                            DatasetsMatchResult.SpaceProgressCount = 0;
                            DatasetsMatchResult.SpaceProgressValue = 0;
                            break;
                        case 2:
                            DatasetsMatchResult.AttributeProgressCount = 0;
                            DatasetsMatchResult.AttributeProgressValue = 0;
                            break;
                        case 3:
                            DatasetsMatchResult.TopollogyProgressCount = 0;
                            DatasetsMatchResult.TopollogyProgressValue = 0;
                            break;
                        case 5:
                            DatasetsMatchResult.FusionProgressCount = 0;
                            DatasetsMatchResult.SetsRecordCountSum = 0;
                            if (this.Sets_CheckBox.Checked)
                            {
                                foreach (DatasetVector[] setToset in m_SetToSets)
                                {
                                    DatasetsMatchResult.SetsRecordCountSum += m_dataInfo.GetSelectionDataIDs(this.Set1_Combo.Text, setToset[0].Name).Length;
                                }
                            }
                            else
                            {
                                foreach (DatasetVector[] setToset in m_SetToSets)
                                {
                                    DatasetsMatchResult.SetsRecordCountSum += setToset[0].RecordCount;
                                }
                            }
                            break;
                        default:
                            break;
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }
        }
        #endregion
    }
}
